<template>
  <div v-drag>
    <div class="mask"  @click="closePopup(false)"></div>
    <div class="edit_box" v-loading="loading">
      <div class="box_head">
        <p>绑定仓库</p>
        <i  @click="closePopup(false)"></i>
      </div>
      <div class="box_body" >

        <div class="line">
          <span class="lab">地址：</span>
          <el-input v-model="address" type="textarea" disabled></el-input>
        </div>

        <div class="line" :class="{'error_border':tip_num===1}">
          <span class="lab"><i class="mark_icon">*</i>仓库：</span>
          <div>
            <el-select
              v-model="newWarehouseId"
              filterable
              @focus="setTip('','')"
              placeholder="请选择配仓库">
              <el-option
                v-for="(item,index) in warehouseOption"
                :key="index"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </div>
        </div>
        <p class="error_line">{{infoTip}}</p>
      </div>
      <div class="box_foot">
        <span @click="closePopup(false)">取消</span>
        <span @click="confirmEdit">确定</span>
      </div>
    </div>
  </div>
</template>

<script>
import { changeWarehouse,getDetail } from "@/api/data/user/address";
import {getAllWarehouse} from "@/api/basic/warehouse";
export default {
  props:['addressId','address'],
  data(){
    return {
      newWarehouseId: '',
      infoTip: '',
      tip_num: '',
      loading:false,
      warehouseOption:[]
    }
  },
  mounted() {
    this.getWarehouse();
    if (this.addressId){
      this.getDetail();
    }
  },
  methods:{
    //获取原绑定的仓库id
    getDetail(){
      let that = this;
      this.loading = true;
      getDetail(that.addressId).then(res => {
        if (res){
          this.newWarehouseId = res.warehouseId?res.warehouseId:'';
        }
        that.loading = false;
      }).catch((e) => {
        that.loading = false;
      });
    },

    getWarehouse(){
      getAllWarehouse('').then((res)=>{
        this.warehouseOption = res ? res: [];
      })
    },
    // 确认编辑
    confirmEdit(){
      if (this.newWarehouseId === '') {
        this.setTip(1, '请选择仓库！');
      } else {
        this.sendInfo();
      }
    },

    sendInfo(){
      let that = this;
      that.loading = true;
      let data = {
        id: that.addressId,
        warehouseId:that.newWarehouseId
      }
      changeWarehouse(data).then(response => {
          that.closePopup(true);
          that.$message.success({
            message:'绑定成功！',
            duration:1500
          });
          that.loading = false;
        }).catch((e) => {
          that.loading = false;
        });

    },

    setTip(num,text){
      this.tip_num = num;
      this.infoTip = text
    },

    // 关闭
    closePopup(refresh){
      this.$emit('listenToChild', {refresh: refresh})
    }
  }
}
</script>

<style scoped>


</style>

